package cn.edu.swu;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

public class RegServlet extends HttpServlet {

    private static final long serialVersionUID = -17308446430189559L;

    private static final String INSERT_TEMPLATE =
            "INSERT INTO aitems (`name`, `password`) " +
                    "VALUES ('%s', '%s')";

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String pass = request.getParameter("pass");
        String repass = request.getParameter("repass");
        String Rcode = request.getParameter("Rcode");


        if(name==null||pass==null||repass==null){
            return;
        }
        //判断两次密码是否一致
        if(!pass.equals(repass)){
            return;
        }

        if(!Rcode.equals("qwer123")){
           return;
        }

        String sql = String.format(INSERT_TEMPLATE, name,pass);
        System.out.println(sql);



        try {
            DBUtils.insert2(sql);
            response.sendRedirect("/myapp/login.html");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}



